<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索和添加 -->
    <div style="margin-top: 15px;">
      <el-button type="success" plain @click="showAddRoleDialog">添加商品</el-button>
    </div>
    <tree-grid :treeStructure="true" :columns="columns" :data-source="dataSource" @deleteCate="deleteCategory" @editCate="editCategory">
    </tree-grid>
    <!-- 添加商品对话框 -->
    <el-dialog title="添加商品分类" :visible.sync="adddialogFormVisible">
      <el-form :model="addform">
        <el-form-item label="分类名称">
          <el-input v-model="addform.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="父级名称">
          <el-cascader :options="options" v-model="selectedOptions" @change="handleChange" :props="defaultProps">
          </el-cascader>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="adddialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getCategoriesList } from '@/api/index.js'
import TreeGrid from '@/components/TreeGrid/TreeGrid.vue'
export default {
  // 注入想用的插件
  components: { 'tree-grid': TreeGrid },
  data () {
    return {
      defaultProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      options: [],
      selectedOptions: [],
      adddialogFormVisible: false,
      addform: {
        name: '',
        region: ''
      },
      dataSource: [],
      columns: [
        {
          text: '分类名称',
          dataIndex: 'cat_name',
          width: ''
        },
        {
          text: '是否有效',
          dataIndex: 'cat_deleted',
          width: ''
        },
        {
          text: '排序',
          dataIndex: 'cat_level',
          width: ''
        }
      ]
    }
  },
  mounted () {
    getCategoriesList(3).then(res => {
      console.log(res)
      this.dataSource = res.data
    })
  },
  methods: {
    handleChange (value) {
      console.log(value)
    },
    //   添加分类对话框
    showAddRoleDialog () {
      this.adddialogFormVisible = true
      getCategoriesList(2).then(res => {
        console.log(res)
        this.options = res.data
      })
    },
    deleteCategory (cid) {
      console.log(cid)
    },
    editCategory (cid) {
      console.log(cid)
    }
  }
}
</script>

<style lang="scss" scoped>
.el-breadcrumb {
  background-color: #f5f5f5;
  height: 45px;
  font-size: 15px;
  padding-left: 10px;
  line-height: 45px;
  margin-bottom: 15px;
}
</style>
